package com.happystudy.serviceImpl;

import com.happpystudy.model.User;
import com.happpystudy.model.course.CourseJoinStudent;
import com.happpystudy.model.course.CourseKey;
import com.happpystudy.model.course.CourseTeacher;
import com.happpystudy.service.CourseJoinStudentService;
import com.happpystudy.service.UserService;
import com.happystudy.mapper.CourseJoinStudentMapper;
import com.happystudy.util.KeyUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class CourseJoinStudentServiceImpl implements CourseJoinStudentService {

    @Resource
    CourseJoinStudentMapper courseJoinStudentMapper;

    @Resource
    UserService userService;
    @Resource
    RedisTemplate redisTemplate;

    /**
     * 将courseJoinStudent插入到数据表中
     * @param courseJoinStudent 插入的courseJoinStudent
     * @return 插入的条数
     */
    @Override
    @Transactional
    public int insertCourseJoinStudent(CourseJoinStudent courseJoinStudent) {
        int result;
        try {
            result = courseJoinStudentMapper.insertSelective(courseJoinStudent);
        }catch (SQLIntegrityConstraintViolationException e){
            result = -3;
        }catch (Exception ex){
            ex.printStackTrace();
            result=-4;
        }

        if (result>0){
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(CourseJoinStudent.class));
            String redisKey = KeyUtil.COURSE_STUDENT_KEY +":"+courseJoinStudent.getUserId().toString();
            redisTemplate.opsForSet().add(redisKey,courseJoinStudent);
        }
        return result;
    }

    @Override
    public List<User>  selectAllUserByCourseKey(CourseKey courseKey){
        List<User> users = new ArrayList<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(CourseJoinStudent.class));
        String key = KeyUtil.COURSE_STUDENT_BY_COURSE_KEY+":"+courseKey.getUniId()+courseKey.getCourseId();
        Set<CourseJoinStudent> courseTeacherSet =  redisTemplate.opsForSet().members(key);
        List<CourseJoinStudent> courseTeachers = new ArrayList<>();
        if (courseTeacherSet!=null){
            courseTeachers = new ArrayList<>(courseTeacherSet);
        }
        if (courseTeachers.size() == 0){
            courseTeachers = courseJoinStudentMapper.selectByCourseKey(courseKey);
            if (courseTeachers!=null && courseTeachers.size()!=0){
                for (CourseJoinStudent courseJoinStudent:courseTeachers) {
                    redisTemplate.opsForSet().add(key, courseJoinStudent);
                }
            }
        }


        if (courseTeachers==null){
            return users;
        }
        for (CourseTeacher courseTeacher:courseTeachers){
            User user = userService.queryUserByUserId(courseTeacher.getUserId());
            users.add(user);
        }
        return users;
    }

    @Override
    public List<CourseTeacher> queryAllCourseJoinStudentByUserId(Integer userId) {

        return courseJoinStudentMapper.selectCourseTeachersByUserId(userId);
    }
}
